package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import model.College;
import model.Major;
import util.DBHelper;
//专业业务控制类
public class MajorServlet extends BaseServlet {

	//进入学生管理初始查询所有专业
	public void selectAllMajor(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		Major m = new Major();
		m.getParams(req);
		//计算总行数
		String totalCountSql = m.getTotalCountSql();
		long totalCount = (long)DBHelper.query(totalCountSql).get(0);
		String sql = m.getSelectSql();
		//计算分页起始行
		int limitPageNo = (m.getPageNo() - 1) * m.getRowCount();
		sql += " limit " + limitPageNo + "," + m.getRowCount();
		List<Major> list = DBHelper.query(sql, Major.class, null);
		
		if (m.getmName() != null && !m.getmName().equals("")){
            req.setAttribute("mName",m.getmName());
        }
		if (m.getcId() != null && !m.getcId().equals("")){
            req.setAttribute("cId",m.getcId());
        }
		//查询所有学院
		String sql2 = "select * from college";
		List<College> allColleges = DBHelper.query(sql2, College.class, null);
		
		req.setAttribute("list", list);
		req.setAttribute("allColleges", allColleges);//学院列表
		//添加分页参数
		req.setAttribute("pageNo", m.getPageNo());
		//计算总页数
		int totalPage = (int)(totalCount / m.getRowCount());
		if (totalCount % m.getRowCount() != 0 ) {
			totalPage += 1;
		}
		req.setAttribute("totalPage", totalPage);
		req.getRequestDispatcher("maj_mana.jsp").forward(req, res);
	}
	//点击更新，跳转更新专业页面，将数据库里专业信息传给表格
	public void updateMajorPage(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		Major m = new Major();
		m.getParams(req);
		
		//查询所有学院
		String sql2 = "select * from college";
		List<College> allColleges = DBHelper.query(sql2, College.class, null);
		//组装sql
		String sql = m.getSelectSql();
//		System.out.println(sql);
		List<Major> list = DBHelper.query(sql, Major.class, null);
//		System.out.println(list);
		req.setAttribute("obj", list.get(0));
		req.setAttribute("allColleges", allColleges);
		req.getRequestDispatcher("update_maj.jsp").forward(req, res);
	}
	//跳转添加专业页面
	public void addMajorPage(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		//查询所有学院
		String sql = "select * from college";
		List<College> allColleges = DBHelper.query(sql, College.class, null);

		req.setAttribute("allColleges", allColleges);
		req.getRequestDispatcher("add_maj.jsp").forward(req, res);
	}	
	//点击添加，跳转添加专业页面
	public void addOneMajor(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		System.out.println("addOneMajor");
		Major m = new Major();
		m.getParams(req);
		//组装sql
		String sql = m.getInsertSql();
//		System.out.println(sql);
		int count = DBHelper.execute(sql, null);
		
		PrintWriter out = res.getWriter();
		if (count > 0) {
			out.write("1");
		}else {
			out.write("0");
		}
		out.flush();
		out.close();
	}
	//查询学院的信息，实现二级联动
	public void listOfCollegeId(HttpServletRequest req, HttpServletResponse res) throws IOException {
		Integer cId = Integer.valueOf(req.getParameter("cId"));
//		System.out.println(cId);
		String sql = "select * from major where c_id = ?";
		List<Major> list  = DBHelper.query(sql, Major.class, cId);
		//创建json数组
		JSONArray arr = new JSONArray();
		for (Major major : list) {
			//创建json对象
			JSONObject obj = new JSONObject();
			//添加键值对
			obj.put("id", major.getId());
			obj.put("name", major.getmName());
			arr.add(obj); 
		}
		res.setCharacterEncoding("UTF-8");
		PrintWriter out = res.getWriter();
		out.write(arr.toString());
		out.flush();
		out.close();
	}
	//更新专业信息
	public void updataMajor(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		Major m = new Major();
		m.getParams(req);
		//组装sql
		String sql = m.getUpdateSql();

//		System.out.println(sql);
		int count = DBHelper.execute(sql, null);
		
		PrintWriter out = res.getWriter();
		if (count > 0) {
			out.write("1");
		}else {
			out.write("0");
		}
		out.flush();
		out.close();
	}
}
